package com.example.communist_app_api.controller.web;


import com.example.communist_app_api.db.pojoDto.Article;
import com.example.communist_app_api.db.pojoDto.Comment;
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.db.pojoDto.UserSoncomment;
import com.example.communist_app_api.service.*;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.ArticleIdReq;
import com.example.communist_app_api.vo.uniapp.req.ArticleTypeReq;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/article/")
@Api("文章controller控制器")
public class ArticleController{
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTypeService articleTypeService;

    @Autowired
    private CartService cartService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @Autowired
    private UserSoncommentService userSoncommentService;

    @Autowired
    private UserCommentLikeService userCommentLikeService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserOrganizationService userOrganizationService;

    @Autowired
    private OrganizationService organizationService;


    //@PostMapping("/showArticle")
    @ApiOperation("查看全部文章")
    @GetMapping("/showArticle")
    public R showAllArticle(HttpServletRequest httpServletRequest)
    {
        //查看文章用户无需权限，全部放行，不用查看权限
        Integer userId = getUserIdByWeb(httpServletRequest);
        //判断用户所属组织
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        List<Article> articleList;
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            articleList = articleService.getAllArticle();
        }
        else {
            articleList = articleService.getArticleByParentId(organizationId);
        }
        if (articleList == null)
        {
            return R.error("文章为空");
        }
        else
            return R.ok("查询成功").put("articleList",articleList);

    }

    //@PostMapping("/showTopArticle")
    @ApiOperation("查询置顶文章")
    @GetMapping("/showTopArticle")
    public R showTopArticle(HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByWeb(httpServletRequest);
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            List<Article> list  = articleService.getAllTopArticle();
            if (list == null)
            {
                return R.error("文章为空");
            }
            else
                return R.ok("查询成功").put("list",list);
        }
        else
        {
            List<Article> list = articleService.getTopArticle(organizationId);
            if (list == null)
            {
                return R.error("文章为空");
            }
            else
                return R.ok("查询成功").put("list",list);
        }
    }

    @PostMapping("/insertArticle")
    @ApiOperation("添加文章")
    public R insertArticle(@RequestBody Article article,HttpServletRequest httpServletRequest)
    {
        Integer userId = getUserIdByWeb(httpServletRequest);
        article.setUserId(userId);
        int organizationId = userOrganizationService.getOrganizationIdByUserId(userId);
        //拿到用户组织id后需判断组织属于哪一等级
        String OrganizationLevel = organizationService.getOrganizationLevelById(organizationId);
        if (Objects.equals(OrganizationLevel, "党总支"))
        {
            int insertNum = articleService.insertArticle(article);
            if (insertNum == 1){
                return R.ok();
            }
            else return R.error();
        }
        else {
            article.setOrganizationId(organizationId);
            int insertNum = articleService.insertArticle(article);
            if (insertNum == 1){
                return R.ok();
            }
            else return R.error();
        }
    }

    @PostMapping("/isTopArticle")
    @ApiOperation("是否置顶文章")
    public R isTopArticle(@RequestBody ArticleIdReq articleIdReq)
    {
        Boolean top = articleService.updateTopArticle(articleIdReq.getIsTop(),articleIdReq.getId());
        if (top) {
            return R.ok();
        }
        else return R.error();
    }


    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userIdByWeb = userService.getUserInfo(userName).getId();
        return userIdByWeb;
    }
}
